package com.wc.alorithm_luogu.P1205;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/4/21 15:44
 * @description https://www.luogu.com.cn/problem/P1205
 * [USACO1.2] 方块转换 Transformations
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = Integer.parseInt(sc.nextLine());
        String[] originStr = new String[n];
        String[] endStr = new String[n];
        for (int i = 0; i < n; i++) {
            originStr[i] = sc.nextLine();
        }

        for (int i = 0; i < n; i++) {
            endStr[i] = sc.nextLine();
        }
        char[][] originMap = new char[n][n];
        char[][] endMap = new char[n][n];

        for (int i = 0; i < n; i++) {
            originMap[i] = originStr[i].toCharArray();
            endMap[i] = endStr[i].toCharArray();
        }

        Main main = new Main();
        boolean equal = main.isEqual(originMap, endMap);

        main = getMain(originMap, endMap, false);
        if (main == null) return;
        // 旋转360°，回到原位
        main.roll(originMap);

        main.symmetry(originMap);
        if (main.isEqual(originMap, endMap)) {
            System.out.println(4);
            return;
        }
        main = getMain(originMap, endMap, true);
        if (main != null) {
            System.out.println(5);
            return;
        }
        if (equal) {
            System.out.println(6);
            return;
        }
        System.out.println(7);
    }

    private static Main getMain(char[][] originMap, char[][] endMap, boolean flg) {
        Main main = new Main();
        // 旋转90°
        main.roll(originMap);
        if (main.isEqual(originMap, endMap)) {
            if (flg) {
                return main;
            }
            System.out.println(1);
            return null;
        }
        // 旋转180°
        main.roll(originMap);
        if (main.isEqual(originMap, endMap)) {
            if (flg) {
                return main;
            }
            System.out.println(2);
            return null;
        }
        // 旋转270°
        main.roll(originMap);
        if (main.isEqual(originMap, endMap)) {
            if (flg) {
                return main;
            }
            System.out.println(3);
            return null;
        }
        if (flg) {
            return null;
        }
        return main;
    }

    public void showMap(char[][] originMap) {
        for (int i = 0; i < originMap.length; i++) {
            for (int j = 0; j < originMap.length; j++) {
                System.out.print(originMap[i][j]);
            }
            System.out.println();
        }
    }

    /**
     * 旋转90°
     *
     * @param originMap 待旋转数组
     */
    public void roll(char[][] originMap) {
        int n = originMap.length;
        int round = n / 2;
        char tmp;
        for (int i = 0; i < round; i++) {
            for (int j = i; j < n - i - 1; j++) {
                tmp = originMap[i][j];
                originMap[i][j] = originMap[n - 1 - j][i];
                originMap[n - 1 - j][i] = originMap[n - 1 - i][n - 1 - j];
                originMap[n - 1 - i][n - 1 - j] = originMap[j][n - i - 1];
                originMap[j][n - i - 1] = tmp;
            }
        }
    }

    /**
     * 比较是否完全相等
     *
     * @param originMap 待比较1
     * @param endMap    待比较2
     * @return true为相等， false 为不等
     */
    public boolean isEqual(char[][] originMap, char[][] endMap) {
        int n = originMap.length;
        boolean equal = true;
        for (int i = 0; i < n; i++) {
            if (!Arrays.equals(originMap[i], endMap[i])) {
                equal = false;
                break;
            }
        }
        return equal;
    }

    /**
     * 水平翻转
     *
     * @param originMap 待翻转的数组
     */
    public void symmetry(char[][] originMap) {
        int n = originMap.length;
        char tmp;
        int mid = n / 2;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < mid; j++) {
                tmp = originMap[i][j];
                originMap[i][j] = originMap[i][n - 1 - j];
                originMap[i][n - 1 - j] = tmp;
            }
        }
    }
}
